package com.escom.utils;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.escom.R;
import com.escom.entidad.ConsultaItem;

import java.util.List;

/**
 * Created by Carlos on 17/04/2014.
 */
public class CustomConsultaAdapter extends ArrayAdapter<ConsultaItem> {

    private Context context;
    private int resource;
    private List<ConsultaItem> retrieveConsulta;

    public CustomConsultaAdapter(Context context, int resource, List<ConsultaItem> retrieveConsulta) {
        super(context, resource, retrieveConsulta);
        this.context = context;
        this.resource = resource;
        this.retrieveConsulta = retrieveConsulta;
    }


    private static class ConsultaViewHolder {
        TextView tVFechaInicio, tVFechaFinal, tVDetalles, tVHospital, tVArritmia;
        LinearLayout layoutNoExiste, layoutConsulta;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ConsultaViewHolder consultaViewHolder;
        View view = convertView;

        if (view == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            view = inflater.inflate(resource, parent, false);
            consultaViewHolder = new ConsultaViewHolder();
            consultaViewHolder.tVFechaInicio = (TextView) view.findViewById(R.id.tVConsultaFechaInicio);
            consultaViewHolder.tVFechaFinal = (TextView) view.findViewById(R.id.tVConsultaFechaFinal);
            consultaViewHolder.tVDetalles = (TextView) view.findViewById(R.id.tVConsultaDetalles);
            consultaViewHolder.tVHospital = (TextView) view.findViewById(R.id.tVConsultaHospital);
            consultaViewHolder.tVArritmia = (TextView) view.findViewById(R.id.tVConsultaArritmia);
            consultaViewHolder.layoutNoExiste = (LinearLayout) view.findViewById(R.id.layoutNoExiste);
            consultaViewHolder.layoutConsulta = (LinearLayout) view.findViewById(R.id.layoutConsulta);
            view.setTag(consultaViewHolder);
        } else {
            consultaViewHolder = (ConsultaViewHolder) view.getTag();
        }

        ConsultaItem consulta = retrieveConsulta.get(position);
        //Log.d(Constantes.LOG_ID, this.getClass().getName() + " Item: " + position + " " + historia.toString());
        if (consulta.isExist()){
            consultaViewHolder.tVFechaInicio.setText(consulta.getFechaInicio());
            consultaViewHolder.tVFechaFinal.setText(consulta.getFechaFinal());
            consultaViewHolder.tVDetalles.setText(consulta.getDetalles());
            consultaViewHolder.tVHospital.setText(consulta.getHospital());
            consultaViewHolder.tVArritmia.setText(consulta.getTipoArritmia());
            consultaViewHolder.layoutNoExiste.setVisibility(LinearLayout.INVISIBLE);
            consultaViewHolder.layoutConsulta.setVisibility(LinearLayout.VISIBLE);
        }else{
            consultaViewHolder.layoutNoExiste.setVisibility(LinearLayout.VISIBLE);
            consultaViewHolder.layoutConsulta.setVisibility(LinearLayout.INVISIBLE);
        }
        return view;
    }


}
